var express = require('express');
const sqlQuery = require('../../model/HGmysql');
import sqlQuery from '../../model/HGmysql'
const nodemailer = require('nodemailer');
//导入验证码随机数
const sjs = require('./sjs');
let mailTransport = nodemailer.createTransport({
    // host: 'smtp.qq.email',
    service: 'qq',
    secure: true, //安全方式发送,建议都加上
    auth: {
        user: '3369824473@qq.com',
        pass: 'lynvzwsguinxchbc'
    }
})
var router = express.Router();
//发送qq邮箱
router.get('/ver', async(req, res) => {
        let numbernumber = sjs().toLowerCase()
            //获取前台数据
        let { email } = req.query
        try {
            let time = new Date().getTime()
            let sql = 'insert into register(eamil,yzm,yzm_timers) values(?,?,?)'
            await sqlQuery(sql, [email, numbernumber, time])
                //发送
            let options = {
                from: '"ulo博客" <3369824473@qq.com>',
                to: `${email}`,
                subject: '注册ulo博客',
                text: `【ulo博客】您正在注册ulo博客！您的验证码是：${numbernumber}，验证码5分钟内有效，切忽泄露给他人，如非本人操作忽略。`,
            };
            mailTransport.sendMail(options, function(err, msg) {
                if (err) {
                    console.log(err);
                    res.send(err);
                } else {
                    res.send('success');
                }
            })

        } catch (error) {
            console.log(error);
        }
    })
    //注册
router.post('/register', async(req, res) => {
    const registerTime = new Date().getTime()

    //接收数据
    console.log(req.body);
    let { username, pass, email, yzmy } = req.body.data
    try {

        //查询邮箱和用户名是否被注册
        let user = await sqlQuery('select * from user where username = ? OR eamil = ?', [username, email]);
        if (!user || user.length === 0) {
            let sql2 = `select * from register where eamil = ? and yzm = ?`
            let result = await sqlQuery(sql2, [email, yzmy])
            let sql3 = `select * from register where eamil = ?`
            let result3 = await sqlQuery(sql3, [email])
            console.log(result3[0].yzm_timers);
            if (registerTime - result3[0].yzm_timers >= 5 * 1000 * 60) {
                await sqlQuery('delete from register where eamil = ?', [email])
                res.send({
                    code: -1,
                    msg: '验证码已过期'
                })
                return
            }
            if (!result || result.length === 0) {
                res.send({ code: -1, msg: '验证码错误' })
            } else {
                await sqlQuery('insert into user(username,password,nickname,eamil,shijian) value(?,?,?,?,NOW())', [username, pass, username, email])
                await sqlQuery('delete from register where eamil = ?', [email])
                res.send({ code: 0, msg: "注册成功！" })
            }
        } else {
            res.send({ code: -1, msg: "用户名或邮箱已存在，请重新注册！" })
        }
    } catch (error) {
        console.log(error);
    }
})

module.exports = router;